<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * User_Autologin
 *
 * This model represents user autologin data. It can be used
 * for user verification when user claims his autologin passport.
 *
 * @package	Tank_auth
 * @author	Ilya Konyukhov (http://konyukhov.com/soft/)
 */
class Kunder_order extends BIT_Model {

	protected $id;

	protected $ipnummer;
	
	protected $datum;
	
	protected $fnamn; 
	
	protected $enamn;
	
	protected $email;
	
	protected $reklam;
	
	protected $telefon;
	
	protected $gadress;
	
	protected $postnr;
	
	protected $postort;

	protected $msg;

	protected $order;

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * Get user data for auto-logged in user.
	 * Return NULL if given key or user ID is invalid.
	 *
	 * @param	int
	 * @param	string
	 * @return	object
	 */
	function getOrdersByCustomer($email, $limit=999) {
		$db = get_instance()->db;

		// Give back all recipes where i.e. Lax is main ingredient, order by name and limit if parameter is set
		return $db->where('email', $email)->limit($limit)->order_by('id','desc')->get('kunder_order')->result();
	}

	function getFullname($reversed = false) {
		if($reversed) {
			return $this->enamn .", ".$this->fnamn;
		} else {
			return $this->fnamn ." ".$this->enamn;
		}
	}

	function getEmailLink() {
		return "<a href='mailto:".$this->email."'>".$this->email."</a>";
	}

	function saveOrder($data) {
		// Check that we have data
		if(!is_array($data)) {
			return false;
		} else {
			// Only set variables that we had before
			foreach($data as $k=>$v){
				if($this[$k]){
					$this[$k] = $v;
				}
			}
			// Return state of save
			return $this->save();
		}
	}
}